package com.qianfeng.openapi.web.master.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.bean.AjaxResponse;
import com.qianfeng.openapi.web.master.bean.TableData;
import com.qianfeng.openapi.web.master.pojo.AppInfo;
import com.qianfeng.openapi.web.master.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/1 8:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/appinfo")
@CrossOrigin(allowCredentials = "true")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;

    @RequestMapping("/table")
    public TableData<AppInfo> table(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "5") Integer limit) {
        PageInfo<AppInfo> customerByPage = appInfoService.getCustomerByPage(page, limit);
        TableData<AppInfo> tableData = new TableData<>();
        tableData.setCount(customerByPage.getTotal());
        tableData.setData(customerByPage.getList());
        return tableData;
    }

    @RequestMapping("/add")
    public AjaxResponse add(AppInfo appInfo) {
        try {
            appInfoService.insertAppInfo(appInfo);
            return new AjaxResponse(true, "添加成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new AjaxResponse(false, "添加失败");
        }
    }

    @RequestMapping("/delete")
    public AjaxResponse delete(Long[] ids) {

        try {
            appInfoService.deleteAppInfos(ids);
            return new AjaxResponse(true, "删除成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new AjaxResponse(false, "删除失败");
        }
    }

    @RequestMapping("/update")
    public AjaxResponse update(AppInfo appInfo) {
        try {
            appInfoService.updateAppInfo(appInfo);
            return new AjaxResponse(true, "更新成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new AjaxResponse(false, "更新失败");
        }
    }

    @RequestMapping("/search")
    public TableData<AppInfo> search(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "5") Integer limit,
                                     String appName, Integer state) {

        PageInfo<AppInfo> pageInfo = appInfoService.selectAppInfoByCondition(page, limit, appName, state);
        TableData<AppInfo> tableData = new TableData<>();
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;
    }


    @RequestMapping("/tree")
    public List<AppInfo> tree(){
        return appInfoService.getAllAppInfos();
    }


}
